package com.yunji.product.convert;

import com.yunji.common.utils.ConvertDataUtil;
import com.yunji.product.dao.IProductDao;
import com.yunji.product.domain.ProductDO;
import com.yunji.product.domain.ProductAgentDO;
import com.yunji.product.dto.ProductAgentPriceDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ProductAgentConvert {

    public static List<ProductAgentPriceDTO> domainToDTO(List<ProductAgentDO> productAgentList, IProductDao productDao) {
        List<ProductAgentPriceDTO> dtoList = new ArrayList<>();
        List<Long> productIds = productAgentList.stream().map(a -> a.getProductId()).collect(Collectors.toList());
        Map<Long, ProductDO> dataMap = ConvertDataUtil.getDataMap(() -> productDao.listByIds(productIds), ProductDO::getId);

        for (ProductAgentDO productAgent : productAgentList) {
            ProductAgentPriceDTO productAgentPriceDTO = new ProductAgentPriceDTO(productAgent, dataMap.get(productAgent.getId()));
            dtoList.add(productAgentPriceDTO);
        }
        return dtoList;
    }

}
